package com.zhiyou.app.shop.common;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.manager.dao.pojo.UserDetail;
import com.zhiyou.app.shop.website.action.LoginAction;

@SuppressWarnings("serial")
public class SessionIterceptor extends AbstractInterceptor {

	private static Log loger = LogFactory.getLog(SessionIterceptor.class);
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		loger.debug("user login.....");

		ActionContext ctx = invocation.getInvocationContext();
        
		//获取session
		//SessionMap<String,Object> session = (SessionMap<String,Object>) ActionContext.getContext().get(ActionContext.SESSION);
        Map session=ctx.getSession();
        //获取拦截器对象
        Object action = (Action) invocation.getAction();
        //如果获取的拦截器为 AdminLoginAction或者UserLoginAction,则不进行拦截
        if(action instanceof LoginAction){
        	return invocation.invoke();
        }

        UserDetail user = (UserDetail) session.get(ShopConstants.LOGIN_USER);
        
        if (user == null) {
        	ctx.put("loginMsg", "登陆超时，请重新登陆。");
        	ctx.put("timeout", "1");
            return Action.LOGIN;
        }
		return invocation.invoke();
	}

}
